package com.ruoyi.realEstate.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.basic.domain.AtbShap;
import com.ruoyi.basic.mapper.AtbShapMapper;
import com.ruoyi.common.filter.ID;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.objectPic.domain.AtbObjectPic;
import com.ruoyi.objectPic.mapper.AtbObjectPicMapper;
import com.ruoyi.realEstate.domain.AtbRealEstateDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.realEstate.mapper.AtbRealEstateMapper;
import com.ruoyi.realEstate.domain.AtbRealEstate;
import com.ruoyi.realEstate.service.IAtbRealEstateService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 地产Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-16
 */
@Service
public class AtbRealEstateServiceImpl implements IAtbRealEstateService 
{
    @Autowired
    private AtbRealEstateMapper atbRealEstateMapper;

    @Autowired
    private  AtbShapMapper atbShapMapper;


    @Autowired
    private AtbObjectPicMapper atbObjectPicMapper;

    /**
     * 查询地产
     * 
     * @param id 地产主键
     * @return 地产
     */
    @Override
    public AtbRealEstate selectAtbRealEstateById(Long id)
    {
        return atbRealEstateMapper.selectAtbRealEstateById(id);
    }

    /**
     * 查询地产列表
     * 
     * @param atbRealEstate 地产
     * @return 地产
     */
    @Override
    public List<AtbRealEstate> selectAtbRealEstateList(AtbRealEstate atbRealEstate)
    {
        return atbRealEstateMapper.selectAtbRealEstateList(atbRealEstate);
    }


    /**
     * 新增地产
     * 
     * @param atbRealEstateDto 地产
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAtbRealEstate(AtbRealEstateDto atbRealEstateDto)
    {

//        ID id = new ID();
//        atbRealEstate.setId(id.getId());
//        atbRealEstate.setCreateTime(DateUtils.getNowDate());
        ID id = new ID();
        long id1 = id.getId();

        AtbShap atbShap = new AtbShap();
        atbShap.setId(id1);
        atbShap.setShopMessage(atbRealEstateDto.getShopMessage());
        atbShap.setShopStatus(atbRealEstateDto.getShopStatus());
        atbShap.setShopArea(atbRealEstateDto.getShopArea());
        atbShap.setPrice(atbRealEstateDto.getPrice());
        atbShap.setShopDistance(atbRealEstateDto.getShopDistance());
        atbShap.setCity1(atbRealEstateDto.getCity1());
        atbShap.setCity2(atbRealEstateDto.getCity2());
        atbShap.setCity3(atbRealEstateDto.getCity3());
        atbShap.setShopDistance(atbRealEstateDto.getShopDistance());
        atbShap.setMovies(atbRealEstateDto.getMovies());
        atbShapMapper.insertAtbShap(atbShap);


        AtbRealEstate atbRealEstate = BeanUtil.toBean(atbRealEstateDto, AtbRealEstate.class);
        atbRealEstate.setId(id1);
        atbRealEstate.setCreateTime(DateUtils.getNowDate());
        atbRealEstate.setUpdateTime(DateUtils.getNowDate());
        atbRealEstate.setEstateType(atbRealEstateDto.getEstateType());
        atbRealEstate.setEstateName(atbRealEstateDto.getEstateName());


        String[] split = atbShap.getMovies().split(",");
        for (String s : split) {

            //图片添加
            AtbObjectPic atbObjectPic = new AtbObjectPic();
            atbObjectPic.setUrl(s);
            atbObjectPic.setObjectId(atbShap.getId());
            atbObjectPicMapper.insertAtbObjectPic(atbObjectPic);
        }




        return atbRealEstateMapper.insertAtbRealEstate(atbRealEstate);
    }

    /**
     * 修改地产
     * 
     * @param atbRealEstate 地产
     * @return 结果
     */
    @Override
    public int updateAtbRealEstate(AtbRealEstate atbRealEstate)
    {
        return atbRealEstateMapper.updateAtbRealEstate(atbRealEstate);
    }

    /**
     * 批量删除地产
     * 
     * @param ids 需要删除的地产主键
     * @return 结果
     */
    @Override
    public int deleteAtbRealEstateByIds(Long[] ids)
    {
        for (Long id : ids) {
            List<AtbObjectPic> atbObjectPicList  = atbObjectPicMapper.selectList(id);

            for (AtbObjectPic atbObjectPic : atbObjectPicList) {
                atbObjectPicMapper.deleteAtbObjectPicById(atbObjectPic.getId());
            }

        }
        atbShapMapper.deleteAtbShapByIds(ids);
        return atbRealEstateMapper.deleteAtbRealEstateByIds(ids);
    }

    /**
     * 删除地产信息
     * 
     * @param id 地产主键
     * @return 结果
     */
    @Override
    public int deleteAtbRealEstateById(Long id)
    {

        atbShapMapper.deleteAtbShapById(id);
        //根据id查看图片图片集合

        List<AtbObjectPic> atbObjectPicList  = atbObjectPicMapper.selectList(id);

        for (AtbObjectPic atbObjectPic : atbObjectPicList) {
            atbObjectPicMapper.deleteAtbObjectPicById(atbObjectPic.getId());
        }
        return atbRealEstateMapper.deleteAtbRealEstateById(id);
    }
}
